package sbcc.edu.blackjack.client;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.Socket;
import java.net.UnknownHostException;

import sbcc.edu.blackjack.MainWindow;
import sbcc.edu.blackjack.Player;
import sbcc.edu.blackjack.command.CommandQueue;
import sbcc.edu.blackjack.command.table.DoubleDownCommand;
import sbcc.edu.blackjack.command.table.HitCommand;
import sbcc.edu.blackjack.command.table.StayCommand;

public class ClientMain {
	private static boolean quit = false;
	private static Player player;

	public static void main(String[] args) throws IOException{
		//get the server to connect to
		//launch an IOthread
		
        Socket clientSocket = null;
        BufferedReader in = null;
        String hostname = "localHost";
        CommandQueue cq = new CommandQueue();
        MainWindow mainWindow;
        try {
           clientSocket = new Socket(hostname, 4444);
          //  out = new PrintWriter(clientSocket.getOutputStream(), true);
           in = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
      } catch (UnknownHostException e) {
            System.err.println("Don't know about host: " + hostname);
            System.exit(1);
        } catch (IOException e) {
           System.err.println("Couldn't get I/O for the connection to host.");
           System.exit(1);
       }

        
         player = new Player(cq);
        ClientOperator operator = new ClientOperator(player);
        new Thread(operator).start();

        //TODONE launch a new MainWindow for the Interface.
        mainWindow = new MainWindow(operator);

        System.out.println("connected to host:" + hostname);
        
        //waiting for input from the interface
        while (!quit ) {            
        	//TODO get input somehow
        }
        in.close();
        clientSocket.close();

	}
	
	public static void quit() {
	quit = true;	
	}
	public void Hit() {
		player.sendCommand(new HitCommand());
	}
	public void Stay() {
		player.sendCommand(new StayCommand());
	}
	public void DoubleDown() {
		player.sendCommand(new DoubleDownCommand());
	}
}
